<?php

	/*
	Plugin Name: ToboAds widget
	Description: Adds a sidebar widget with ToboAds (www.toboads.com) by Tobonet (www.tobonet.com)
	Version: 2.0
	Author: Domen Kosir domen.kosir@tobonet.com
	Author URI: http://domn.sopca.com
	*/

/**
 * ToboAdsWidget Class
 */
class ToboAdsWidget extends WP_Widget
{

	private $toboads_allowed_ad_sizes = array('180x150','234x60','300x250','336x280','468x60','728x90','120x600','120x240','160x240','125x125','160x600','250x250');
	private $toboads_counter_max = 3;
	private $toboads_counter = 0;

	/** constructor */
	function ToboAdsWidget()
	{
		parent::WP_Widget(false, $name = 'ToboAdsWidget');
	}

	/** @see WP_Widget::widget */
	function widget($args, $instance)
	{
		extract($args);
		
		$publisher_id    = $instance['publisher_id'];
		$ad_size         = $instance['ad_size'];
		$open_new_window = ($instance['open_new_window'] == '1' ? '1' : '0');
		$type_text       = ($instance['type_text'] !== '0' ? '1' : '0');
		$type_shop       = ($instance['type_shop'] !== '0' ? '1' : '0');
		$type_rich       = ($instance['type_rich'] !== '0' ? '1' : '0');
		$type_video      = ($instance['type_video'] !== '0' ? '1' : '0');
		$color_border    = $instance['color_border'];
		$color_bg        = $instance['color_bg'];
		$color_title     = $instance['color_title'];
		$color_text      = $instance['color_text'];
		$color_link      = $instance['color_link'];
		$channel         = $instance['channel'];
		
		$string = "<div align=\"center\">\n";
		$string .= "<!-- Httpool Block -->\n";
		$string .= "<script language=\"javascript\" type=\"text/javascript\">\n";
		$string .= "<!--\n";
		$string .= "	httpool_publisher_id = '$publisher_id';\n";
		$string .= "    httpool_block_type = '1';\n";
		$string .= "	httpool_ad_format = '$ad_size';\n";
		$string .= "	httpool_open_new_window = '".($open_new_window == "1" ? "true" : "false")."';\n";
		$string .= "	httpool_ad_type_text = '".($type_text == "1" ? "true" : "false")."';\n";
		$string .= "	httpool_ad_type_shop = '".($type_shop == "1" ? "true" : "false")."';\n";
		$string .= "	httpool_ad_type_rich = '".($type_rich == "1" ? "true" : "false")."';\n";
		$string .= "	httpool_ad_type_video = '".($type_video == "1" ? "true" : "false")."';\n";
		if (strlen($color_border) > 0)
			$string .= "	httpool_color_border = '$color_border';\n";
		if (strlen($color_bg) > 0)
			$string .= "	httpool_color_bg = '$color_bg';\n";
		if (strlen($color_title) > 0)
			$string .= "	httpool_color_title = '$color_title';\n";
		if (strlen($color_link) > 0)
			$string .= "	httpool_color_link = '$color_link';\n";
		if (strlen($color_text) > 0)
			$string .= "	httpool_color_text = '$color_text';\n";
		$string .= "	httpool_channel = '$channel';\n";
		$string .= "-->\n";
		$string .= "</script>\n";
		$string .= "<script language=\"javascript\" type=\"text/javascript\" src=\"http://relay-si.toboads.com/\"></script>\n";
		$string .= "<!-- /Httpool Block -->\n";
		$string .= "</div>\n";
		
		if ($this->toboads_counter < $this->toboads_counter_max && $this->toboads_is_valid_publisher_id($publisher_id))
		{
			echo $before_widget;
			echo $string;
			echo $after_widget;
			$this->toboads_counter++;
		}
	}

	/** @see WP_Widget::update */
	function update($new_instance, $old_instance)
	{
		$instance = $old_instance;
		
		if ($this->toboads_is_valid_publisher_id($new_instance['publisher_id']))
			$instance['publisher_id'] = $new_instance['publisher_id'];
		
		if (in_array($new_instance['ad_size'], $this->toboads_allowed_ad_sizes)) $instance['ad_size'] = $new_instance['ad_size'];
		if (strlen($instance['ad_size']) == 0) $instance['ad_size'] = '120x240';
		
		$open_new_window = abs(intval($new_instance['open_new_window']));
		$open_new_window = ($open_new_window > 0) ? 1 : 0;
		if ($open_new_window <= 1)
			$instance['open_new_window'] = strval($open_new_window);
		
		$instance['type_text']  = $new_instance['type_text'] == 'on' ? '1' : '0';
		$instance['type_shop']  = $new_instance['type_shop'] == 'on' ? '1' : '0';
		$instance['type_rich']  = $new_instance['type_rich'] == 'on' ? '1' : '0';
		$instance['type_video'] = $new_instance['type_video'] == 'on' ? '1' : '0';
		
		$instance['color_border'] = $this->toboads_validate_color($new_instance['color_border']);
		$instance['color_bg']     = $this->toboads_validate_color($new_instance['color_bg']);
		$instance['color_title']  = $this->toboads_validate_color($new_instance['color_title']);
		$instance['color_text']   = $this->toboads_validate_color($new_instance['color_text']);
		$instance['color_link']   = $this->toboads_validate_color($new_instance['color_link']);
		
		$instance['channel'] = $this->toboads_validate_channel($new_instance['channel-channel']);
		
		return $instance;
	}

	/** @see WP_Widget::form */
	function form($instance)
	{
		$publisher_id    = esc_attr($instance['publisher_id']);
		$ad_size         = esc_attr($instance['ad_size']);
		$open_new_window = esc_attr($instance['open_new_window']);
		$type_text       = esc_attr($instance['type_text']);
		$type_shop       = esc_attr($instance['type_shop']);
		$type_rich       = esc_attr($instance['type_rich']);
		$type_video      = esc_attr($instance['type_video']);
		$color_border    = esc_attr($instance['color_border']);
		$color_bg        = esc_attr($instance['color_bg']);
		$color_title     = esc_attr($instance['color_title']);
		$color_text      = esc_attr($instance['color_text']);
		$color_link      = esc_attr($instance['color_link']);
		$channel         = esc_attr($instance['channel']);
		
		if ($instance == null || sizeof($instance) == 0)
		{
			$open_new_window = '1';
			$type_text       = '1';
			$type_shop       = '1';
			$type_rich       = '1';
			$type_video      = '1';
		}
		
		?>
			<p><label for="<?php echo $this->get_field_id('publisher_id'); ?>"><?php _e('Publisher ID:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('publisher_id'); ?>" name="<?php echo $this->get_field_name('publisher_id'); ?>" type="text" value="<?php echo $publisher_id; ?>" /></label></p>
			<p>
				<label for="<?php echo $this->get_field_id('ad_size'); ?>"><?php _e('Ad size:'); ?></label>
				<select id="<?php echo $this->get_field_id('ad_size'); ?>" name="<?php echo $this->get_field_name('ad_size'); ?>"><?php foreach($this->toboads_allowed_ad_sizes as $tmp_ad_size) { if ($ad_size == $tmp_ad_size) $sel = 'selected="selected"'; else $sel = ''; echo '<option value="'.$tmp_ad_size.'" '.$sel.'>'.$tmp_ad_size.'</option>'; }?></select>
			</p>
			<p>
				<label for="<?php echo $this->get_field_id('open_new_window'); ?>_1"><input class="widefat" id="<?php echo $this->get_field_id('open_new_window'); ?>_1" name="<?php echo $this->get_field_name('open_new_window'); ?>" type="radio" value="1" <?php if ($open_new_window == 1) echo 'checked="checked"'; ?> /> <?php _e('open in new window'); ?></label>
				<br />
				<label for="<?php echo $this->get_field_id('open_new_window'); ?>_0"><input class="widefat" id="<?php echo $this->get_field_id('open_new_window'); ?>_0" name="<?php echo $this->get_field_name('open_new_window'); ?>" type="radio" value="0" <?php if ($open_new_window != 1) echo 'checked="checked"'; ?> /> <?php _e('open in same window'); ?></label>
			</p>
			<p>
				<?php _e('Ad types:'); ?>
				<br />
				<input id="<?php echo $this->get_field_id('type_text'); ?>" name="<?php echo $this->get_field_name('type_text'); ?>" type="checkbox" value="on" <?php if ($type_text == 1) echo 'checked="checked"'; ?> /><label for="<?php echo $this->get_field_id('type_text'); ?>"><?php _e('text'); ?></label>
				&nbsp;
				<input id="<?php echo $this->get_field_id('type_shop'); ?>" name="<?php echo $this->get_field_name('type_shop'); ?>" type="checkbox" value="on" <?php if ($type_shop == 1) echo 'checked="checked"'; ?> /><label for="<?php echo $this->get_field_id('type_shop'); ?>"><?php _e('shop'); ?></label>
				&nbsp;
				<input id="<?php echo $this->get_field_id('type_rich'); ?>" name="<?php echo $this->get_field_name('type_rich'); ?>" type="checkbox" value="on" <?php if ($type_rich == 1) echo 'checked="checked"'; ?> /><label for="<?php echo $this->get_field_id('type_rich'); ?>"><?php _e('rich'); ?></label>
				&nbsp;
				<input id="<?php echo $this->get_field_id('type_video'); ?>" name="<?php echo $this->get_field_name('type_video'); ?>" type="checkbox" value="on" <?php if ($type_video == 1) echo 'checked="checked"'; ?> /><label for="<?php echo $this->get_field_id('type_video'); ?>"><?php _e('video'); ?></label>
			</p>
			<p><label for="<?php echo $this->get_field_id('color_border'); ?>"><?php _e('Border color:'); ?> <input style="width: 70px;" id="<?php echo $this->get_field_id('color_border'); ?>" name="<?php echo $this->get_field_name('color_border'); ?>" type="text" value="<?php echo $color_border; ?>" /></label></p>
			<p><label for="<?php echo $this->get_field_id('color_bg'); ?>"><?php _e('Background color:'); ?> <input style="width: 70px;" id="<?php echo $this->get_field_id('color_bg'); ?>" name="<?php echo $this->get_field_name('color_bg'); ?>" type="text" value="<?php echo $color_bg; ?>" /></label></p>
			<p><label for="<?php echo $this->get_field_id('color_title'); ?>"><?php _e('Title color:'); ?> <input style="width: 70px;" id="<?php echo $this->get_field_id('color_title'); ?>" name="<?php echo $this->get_field_name('color_title'); ?>" type="text" value="<?php echo $color_title; ?>" /></label></p>
			<p><label for="<?php echo $this->get_field_id('color_text'); ?>"><?php _e('Text color:'); ?> <input style="width: 70px;" id="<?php echo $this->get_field_id('color_text'); ?>" name="<?php echo $this->get_field_name('color_text'); ?>" type="text" value="<?php echo $color_text; ?>" /></label></p>
			<p><label for="<?php echo $this->get_field_id('color_link'); ?>"><?php _e('Link color:'); ?> <input style="width: 70px;" id="<?php echo $this->get_field_id('color_link'); ?>" name="<?php echo $this->get_field_name('color_link'); ?>" type="text" value="<?php echo $color_link; ?>" /></label></p>
			<p><label for="<?php echo $this->get_field_id('channel'); ?>"><?php _e('Channel ID:'); ?> <input style="width: 80px;" id="<?php echo $this->get_field_id('channel'); ?>" name="<?php echo $this->get_field_name('channel'); ?>" type="text" value="<?php echo $channel; ?>" /></label></p>
		<?php
	}

	/** validates ToboAds publisher ID**/
	function toboads_is_valid_publisher_id($publisher_id)
	{
		if (preg_match("/^[0-9a-f]{32}$/", $publisher_id) > 0)
			return true;
		return false;
	}

	/** validates ToboAds channel ID **/
	function toboads_validate_channel($channel)
	{
		if (preg_match("/^[0-9a-f]{7}$/", $channel) > 0)
			return $channel;
		return '';
	}

	/** validates color code **/
	function toboads_validate_color($color)
	{
		if ((strlen($color) > 0) && ($color{0} != '#')) $color = '#' . $color;
		if (preg_match("/^#[0-9a-f]{6}$/i", $color) > 0) // RGB code
			return $color;
		return '';
	}

} // class ToboAdsWidget



// register ToboAdsWidget widget
add_action('widgets_init', create_function('', 'return register_widget("ToboAdsWidget");'));

?>